package com.poker.filter;

import org.apache.shiro.SecurityUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: yuanZ;
 * @Date: 2018/8/24 15:06;
 * @Description: Shiro Session过滤器;
**/
public class SessionFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        String uri = httpServletRequest.getRequestURI();
        boolean b = uri.contains("ajaxLogin"); //ajax 登陆 排除
        if (!SecurityUtils.getSubject().isAuthenticated() && !b) {
            // 判断session里是否有用户信息
            if (httpServletRequest.getHeader("x-requested-with") != null
                    && httpServletRequest.getHeader("x-requested-with")
                    .equalsIgnoreCase("XMLHttpRequest")) {
                // 如果是ajax请求响应头会有，x-requested-with
                //httpServletResponse.setHeader("sessionstatus", "timeout");// 在响应头设置session状态
                redirect(httpServletRequest, httpServletResponse);
                return;
            }
        }
        filterChain.doFilter(request, response);
    }

    private void redirect(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("sessionstatus", "timeout");// 在响应头设置session状态
        response.setContentType("text/html");
        PrintWriter out;
        try {
            out = response.getWriter();
            out.print("<script>window.location = '" + request.getContextPath() +  "/login';</script>");
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

}
